package com.geeguo.ebuilder.client.controller.system.excelimport;

import com.geeguo.ebuilder.client.base.core.dictionary.FolderEnum;
import com.geeguo.ebuilder.client.base.core.utils.FileHelper;
import com.geeguo.ebuilder.client.base.security.model.LoginUser;
import com.geeguo.ebuilder.client.base.security.utils.AuthUtils;
import com.geeguo.ebuilder.client.base.tenant.model.DataContext;
import com.geeguo.ebuilder.client.base.tenant.provider.TenantProvider;
import com.geeguo.ebuilder.client.business.system.excelimport.model.ExcelImportQuery;
import com.geeguo.ebuilder.client.business.system.excelimport.model.ExcelImportVO;
import com.geeguo.ebuilder.client.business.system.excelimport.service.ExcelImportService;
import com.geeguo.ebuilder.client.business.system.tempfile.model.TempFileEntity;
import com.geeguo.ebuilder.client.business.system.tempfile.service.TempFileService;
import com.geeguo.ebuilder.core.common.action.ActionResult;
import com.geeguo.ebuilder.core.common.model.vo.FileVO;
import com.geeguo.ebuilder.core.common.model.vo.PageListVO;
import com.geeguo.ebuilder.core.common.utils.key.UUIDCheckUtils;
import com.geeguo.ebuilder.core.common.utils.key.UUIDGenerator;
import com.geeguo.ebuilder.core.security.annotation.RequiresLogin;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

@Tag(name = "Excel导入")
@RestController
public class ExcelImportController {

    @Autowired
    private TempFileService tempFileService;
    @Autowired
    private ExcelImportService excelImportService;

    @Operation(summary = "分页查询")
    @PostMapping("/system/excelImport/list")
    @RequiresLogin
    public ActionResult<PageListVO<ExcelImportVO>> list(ExcelImportQuery query) {
        LoginUser currentUser = AuthUtils.getLoginUser();
        DataContext dataContext = TenantProvider.getDataContext();
        query.setUserId(currentUser.getUserId());
        return ActionResult.page(excelImportService.list(query, dataContext));
    }

    @Operation(summary = "上传Excel导入附件")
    @PostMapping("/system/excelImport/uploadFile")
    @RequiresLogin
    public ActionResult<FileVO> uploadItemFile(@RequestParam("file") MultipartFile file) throws IOException {
        LoginUser currentUser = AuthUtils.getLoginUser();
        DataContext dataContext = TenantProvider.getDataContext();
        FileVO fileVO = FileHelper.upload(dataContext.getTenantCode(), FolderEnum.ExcelImport.getFolder(), file);
        TempFileEntity tempFile = new TempFileEntity();
        tempFile.setId(UUIDGenerator.getId());
        tempFile.setFilePath(fileVO.getFilePath());
        tempFile.setTenantCode(dataContext.getTenantCode());
        tempFile.setCreateTime(new Date());
        tempFile.setCreateUser(currentUser.getUserId());
        if (tempFileService.save(tempFile, dataContext)) {
            fileVO.setTempId(tempFile.getId());
        }
        return ActionResult.success(fileVO);
    }

    @Operation(summary = "删除Excel导入任务")
    @PostMapping("/system/excelImport/remove")
    @RequiresLogin
    public ActionResult<Boolean> remove(@RequestParam("id") String id) {
        if (UUIDCheckUtils.check(id)) {
            LoginUser currentUser = AuthUtils.getLoginUser();
            DataContext dataContext = TenantProvider.getDataContext();
            ExcelImportVO excelImport = excelImportService.get(id, dataContext);
            if (excelImport != null) {
                if (!excelImport.getUserId().equals(currentUser.getUserId())) {
                    return ActionResult.fail("没有该数据的删除权限，不能删除");
                }
                if (excelImportService.remove(id, dataContext)) {
                    return ActionResult.success();
                }
            }
        }
        return ActionResult.fail();
    }

    @Operation(summary = "删除Excel导入附件")
    @PostMapping("/system/excelImport/removeFile")
    @RequiresLogin
    public ActionResult<Boolean> removeFile(@RequestParam("tempFileId") String tempFileId) {
        if (UUIDCheckUtils.check(tempFileId)) {
            LoginUser currentUser = AuthUtils.getLoginUser();
            DataContext dataContext = TenantProvider.getDataContext();
            TempFileEntity tempFile = tempFileService.get(tempFileId, dataContext);
            if (tempFile != null) {
                if (!tempFile.getCreateUser().equals(currentUser.getUserId())) {
                    return ActionResult.fail("没有该数据的删除权限，不能删除");
                }
                if (FileHelper.remove(tempFile.getFilePath()) && tempFileService.remove(tempFileId, dataContext)) {
                    return ActionResult.success();
                }
            }
        }
        return ActionResult.fail();
    }

    @Operation(summary = "获取未完成的Excel导入任务数量")
    @GetMapping("/system/excelImport/getUnfinishedCount")
    @RequiresLogin
    public ActionResult<Integer> getUnfinishedCount() {
        LoginUser currentUser = AuthUtils.getLoginUser();
        DataContext dataContext = TenantProvider.getDataContext();
        return ActionResult.success(excelImportService.countUnfinished(currentUser.getUserId(), dataContext));
    }
}
